package middleware

import (
	"GOPAAS/internal/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/url"
)

func CheckApiLogin() gin.HandlerFunc {
	return func(c *gin.Context) {
		user_id, exist := c.Get("user_id")
		if !exist || user_id.(int64) == 0 {
			utils.HttpError(c, 401, "请先登录", nil)
			c.Abort()
			return
		}
		c.Next()
		return
	}
}

func CheckWebLogin() gin.HandlerFunc {
	return func(c *gin.Context) {
		user_id, exist := c.Get("user_id")
		if !exist || user_id.(int64) == 0 {
			now_url := c.FullPath()
			en_url := url.QueryEscape(now_url)
			c.Redirect(302, fmt.Sprintf("/web/login?redirect_url=%s", en_url))
			c.Abort()
			return
		}
		c.Next()
		return
	}
}
